﻿var @(Model.ConfigInfo.EntityName)Edit = {};
(function(ns){
    ns.initForm = function(){
        if (opera === "Edit") {
			if (id !== null && id !== "") {
				ns.initData(id);
			}
        }
    }
	ns.initData = function (id) {
		$.ajax({
			url: '/@(Model.ConfigInfo.ModelFolderName)Manage/@Model.ConfigInfo.EntityName/GetEntity',
			type: 'post',
			dataType: 'json',
			data: { id: id },
			success: function (data) {
				if (data !== "" && data !== null) {
					$("#form1").fill(data);
				} else {
					showTig("当前数据加载失败!", "error");
				}
			}
		});
        }
    ns.bindEvent = function(){
        // 初始化表单验证插件
        $('#form1').validate({
            submitHandler: ns.saveData,
            errorClass: 'errorMsg',
            errorPlacement: function (error, element) {
                //error.appendTo(element.parent("td").next("td"));
            },
            onkeyup: false
        });
    }
    ns.saveData = function(){
        var obj = $("#form1").serializeForm();
        var entity = JSON.parse(obj);
        $.ajax({
            url: '/@(Model.ConfigInfo.ModelFolderName)Manage/@Model.ConfigInfo.EntityName/SaveEntity',
            type: 'post',
            dataType: 'text',
            data: { inputEntity: entity },
            success: function (data) {
                if (data.length > 0) {
                    parent.@(Model.ConfigInfo.EntityName)Manage.getDataList();
                    parent.closeWindow();
                } else {
                    showTig("已存在！", "error");
                }
            },
            
        });
    }
})(@(Model.ConfigInfo.EntityName)Edit);

$(document).ready(function () {
    @(Model.ConfigInfo.EntityName)Edit.initForm();
    @(Model.ConfigInfo.EntityName)Edit.bindEvent();
})